<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="utf-8">
	<title>工厂模式</title>
	<script type="text/javascript" src="js/commonUtil.js"></script>
</head>
<body>
<script type="text/javascript">
	
	// 搭建一个工厂环境
	// 买车商店
	function CarShop(){}
	CarShop.prototype.sellCar = function (type) {
		//var car = CarFactory.createCar(type);
		// switch(type){
		// 	case 'Bmw': car = new Bmw(); break;
		// 	case 'Benc': car = new Benc(); break;
		// 	case 'Audi': car = new Audi(); break;
		// 	default: 'not bay it'
		// }
		// Jerry.Interface.ensureImplements(car, CarInterface);
		//return car;
		this.abstractSellCar(type);
	}
	CarShop.prototype.abstractSellCar = function (type) {
		throw new Error('not buy it');
	}

	// Bmw 4s店
	function BmwCarShop () {}
	Jerry.extend(BmwCarShop, CarShop);
	BmwCarShop.prototype.sellCar = function (type) {
		var car,
			types = ['Bmw']; //所有Bmw类型的车
		for(index in types){
			if(types[index] === type){
				car = CarFactory.createCar(type);
			} else {
				alert('没有您想要的型号');
			}
		}
		return car;
	}

	// Benc 4s店
	function BencCarShop () {}
	Jerry.extend(BencCarShop, CarShop);
	BencCarShop.prototype.sellCar = function (type) {
		var car,
			types = ['Bmw']; //所有Benc类型的车
		for(index in types){
			if(types[index] === type){
				car = CarFactory.createCar(type);
			} else {
				alert('没有您想要的型号');
			}
		}
		return car;
	}

	// Audi 4s店
	function AudiCarShop () {}
	Jerry.extend(AudiCarShop, CarShop);
	AudiCarShop.prototype.sellCar = function (type) {
		var car,
			types = ['Bmw']; //所有Audi类型的车
		for(index in types){
			if(types[index] === type){
				car = CarFactory.createCar(type);
			} else {
				alert('没有您想要的型号');
			}
		}
		return car;
	}



	//生产车的工厂
	var CarFactory = {
		createCar: function (type) {
			var car = eval('new ' + type + '()');
			// switch(type){
			// 	case 'Bmw': car = new Bmw(); break;
			// 	case 'Benc': car = new Benc(); break;
			// 	case 'Audi': car = new Audi(); break;
			// 	default: 'not buy it'
			// }
			Jerry.Interface.ensureImplements(car, CarInterface);
			return car;
		}
	}

	// 定义接口
	var CarInterface = new Jerry.Interface('CarInterface', ['start', 'run']);

	function BaseCar () {}
	BaseCar.prototype = {
		start: function () {
			alert(this.constructor.name + ' ...start');
			return this;
		},
		run: function () {
			alert(this.constructor.name + ' ...run');
			return this;
		}
	}
	Object.defineProperty(BaseCar.prototype, 'constructor', {
		enumerable: false, //设置能否被枚举
		value: BaseCar
	})

	// 车类
	// 继承注意： 先继承，后定义特有方法
	function Bmw () {}
	Jerry.extend(Bmw, BaseCar);
	Bmw.prototype.driveBmw = function () {alert('Bmw...drive');return this;}
	//Bmw.prototype.start = function () {alert('Bmw...start');return this;}
	//Bmw.prototype.run = function () {alert('Bmw...run');return this;}

	function Benc () {}
	Jerry.extend(Benc, BaseCar);
	Benc.prototype.driveBenc = function () {alert('Benc...drive');return this;}
	//Benc.prototype.start = function () {alert('Benc...start');return this;}
	//Benc.prototype.run = function () {alert('Benc...run');return this;}
	
	function Audi () {}
	Jerry.extend(Audi, BaseCar);
	Audi.prototype.driveAudi = function () {alert('Audi...drive');return this;}
	//Audi.prototype.start = function () {alert('Audi...start');return this;}
	//Audi.prototype.run = function () {alert('Audi...run');return this;}
	
	var shop = new BmwCarShop();
	var car = shop.sellCar('Bmw');
	car.start().run().driveBmw();

</script>
</body>
</html>